package org.jeecg.modules.mes.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.modules.mes.service.IMesStatisticsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * MES生产统计控制器
 * 
 * @author wanke-erp
 * @since 2025-09-15
 */
@Api(tags = "MES生产统计")
@RestController
@RequestMapping("/mes/statistics")
@Slf4j
public class MesStatisticsController {

    @Autowired
    private IMesStatisticsService mesStatisticsService;

    /**
     * 获取生产效率概览统计
     */
    @ApiOperation(value = "获取生产效率概览统计", notes = "获取生产效率概览统计")
    @GetMapping("/production/efficiency/overview")
    public Result<Map<String, Object>> getProductionEfficiencyOverview(
            @RequestParam(required = false) String startDate,
            @RequestParam(required = false) String endDate) {
        try {
            Map<String, Object> result = mesStatisticsService.getProductionEfficiencyOverview(startDate, endDate);
            return Result.OK(result);
        } catch (Exception e) {
            log.error("获取生产效率概览统计失败", e);
            return Result.error("获取生产效率概览统计失败: " + e.getMessage());
        }
    }

    /**
     * 获取生产计划vs实际完成对比数据
     */
    @ApiOperation(value = "获取生产计划vs实际完成对比数据", notes = "获取生产计划vs实际完成对比数据")
    @GetMapping("/production/efficiency/plan-vs-actual")
    public Result<Map<String, Object>> getPlanVsActualData(
            @RequestParam(required = false) String startDate,
            @RequestParam(required = false) String endDate,
            @RequestParam(defaultValue = "month") String timeType) {
        try {
            Map<String, Object> result = mesStatisticsService.getPlanVsActualData(startDate, endDate, timeType);
            return Result.OK(result);
        } catch (Exception e) {
            log.error("获取生产计划vs实际完成对比数据失败", e);
            return Result.error("获取生产计划vs实际完成对比数据失败: " + e.getMessage());
        }
    }

    /**
     * 获取生产效率趋势数据
     */
    @ApiOperation(value = "获取生产效率趋势数据", notes = "获取生产效率趋势数据")
    @GetMapping("/production/efficiency/trend")
    public Result<List<Map<String, Object>>> getProductionEfficiencyTrend(
            @RequestParam(required = false) String startDate,
            @RequestParam(required = false) String endDate,
            @RequestParam(defaultValue = "month") String timeType) {
        try {
            List<Map<String, Object>> result = mesStatisticsService.getProductionEfficiencyTrend(startDate, endDate, timeType);
            return Result.OK(result);
        } catch (Exception e) {
            log.error("获取生产效率趋势数据失败", e);
            return Result.error("获取生产效率趋势数据失败: " + e.getMessage());
        }
    }

    /**
     * 获取工序耗时分析数据
     */
    @ApiOperation(value = "获取工序耗时分析数据", notes = "获取工序耗时分析数据")
    @GetMapping("/production/efficiency/process-time")
    public Result<List<Map<String, Object>>> getProcessTimeAnalysis(
            @RequestParam(required = false) String startDate,
            @RequestParam(required = false) String endDate) {
        try {
            List<Map<String, Object>> result = mesStatisticsService.getProcessTimeAnalysis(startDate, endDate);
            return Result.OK(result);
        } catch (Exception e) {
            log.error("获取工序耗时分析数据失败", e);
            return Result.error("获取工序耗时分析数据失败: " + e.getMessage());
        }
    }

    /**
     * 获取任务状态分布数据
     */
    @ApiOperation(value = "获取任务状态分布数据", notes = "获取任务状态分布数据")
    @GetMapping("/production/efficiency/task-status")
    public Result<List<Map<String, Object>>> getTaskStatusDistribution(
            @RequestParam(required = false) String startDate,
            @RequestParam(required = false) String endDate) {
        try {
            List<Map<String, Object>> result = mesStatisticsService.getTaskStatusDistribution(startDate, endDate);
            return Result.OK(result);
        } catch (Exception e) {
            log.error("获取任务状态分布数据失败", e);
            return Result.error("获取任务状态分布数据失败: " + e.getMessage());
        }
    }

    /**
     * 获取员工效率排行榜
     */
    @ApiOperation(value = "获取员工效率排行榜", notes = "获取员工效率排行榜")
    @GetMapping("/production/efficiency/employee-ranking")
    public Result<List<Map<String, Object>>> getEmployeeEfficiencyRanking(
            @RequestParam(required = false) String startDate,
            @RequestParam(required = false) String endDate,
            @RequestParam(defaultValue = "10") Integer limit) {
        try {
            List<Map<String, Object>> result = mesStatisticsService.getEmployeeEfficiencyRanking(startDate, endDate, limit);
            return Result.OK(result);
        } catch (Exception e) {
            log.error("获取员工效率排行榜失败", e);
            return Result.error("获取员工效率排行榜失败: " + e.getMessage());
        }
    }

    /**
     * 获取人员效率概览统计
     */
    @ApiOperation(value = "获取人员效率概览统计", notes = "获取人员效率概览统计")
    @GetMapping("/personnel/efficiency/overview")
    public Result<Map<String, Object>> getPersonnelEfficiencyOverview(
            @RequestParam(required = false) String startDate,
            @RequestParam(required = false) String endDate) {
        try {
            Map<String, Object> result = mesStatisticsService.getPersonnelEfficiencyOverview(startDate, endDate);
            return Result.OK(result);
        } catch (Exception e) {
            log.error("获取人员效率概览统计失败", e);
            return Result.error("获取人员效率概览统计失败: " + e.getMessage());
        }
    }

    /**
     * 获取员工产量排行
     */
    @ApiOperation(value = "获取员工产量排行", notes = "获取员工产量排行")
    @GetMapping("/personnel/efficiency/production-ranking")
    public Result<List<Map<String, Object>>> getEmployeeProductionRanking(
            @RequestParam(required = false) String startDate,
            @RequestParam(required = false) String endDate,
            @RequestParam(defaultValue = "10") Integer limit) {
        try {
            List<Map<String, Object>> result = mesStatisticsService.getEmployeeProductionRanking(startDate, endDate, limit);
            return Result.OK(result);
        } catch (Exception e) {
            log.error("获取员工产量排行失败", e);
            return Result.error("获取员工产量排行失败: " + e.getMessage());
        }
    }

    /**
     * 获取工时利用率统计
     */
    @ApiOperation(value = "获取工时利用率统计", notes = "获取工时利用率统计")
    @GetMapping("/personnel/efficiency/work-hour-utilization")
    public Result<List<Map<String, Object>>> getWorkHourUtilization(
            @RequestParam(required = false) String startDate,
            @RequestParam(required = false) String endDate) {
        try {
            List<Map<String, Object>> result = mesStatisticsService.getWorkHourUtilization(startDate, endDate);
            return Result.OK(result);
        } catch (Exception e) {
            log.error("获取工时利用率统计失败", e);
            return Result.error("获取工时利用率统计失败: " + e.getMessage());
        }
    }

    /**
     * 获取技能分布统计
     */
    @ApiOperation(value = "获取技能分布统计", notes = "获取技能分布统计")
    @GetMapping("/personnel/efficiency/skill-distribution")
    public Result<List<Map<String, Object>>> getSkillDistribution(
            @RequestParam(required = false) String startDate,
            @RequestParam(required = false) String endDate) {
        try {
            List<Map<String, Object>> result = mesStatisticsService.getSkillDistribution(startDate, endDate);
            return Result.OK(result);
        } catch (Exception e) {
            log.error("获取技能分布统计失败", e);
            return Result.error("获取技能分布统计失败: " + e.getMessage());
        }
    }

    /**
     * 获取培训效果分析
     */
    @ApiOperation(value = "获取培训效果分析", notes = "获取培训效果分析")
    @GetMapping("/personnel/efficiency/training-effect")
    public Result<List<Map<String, Object>>> getTrainingEffectAnalysis(
            @RequestParam(required = false) String startDate,
            @RequestParam(required = false) String endDate) {
        try {
            List<Map<String, Object>> result = mesStatisticsService.getTrainingEffectAnalysis(startDate, endDate);
            return Result.OK(result);
        } catch (Exception e) {
            log.error("获取培训效果分析失败", e);
            return Result.error("获取培训效果分析失败: " + e.getMessage());
        }
    }
}
